package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.UserCommentVo;
import com.itheima.domain.vo.UserLikeCountVo;
import com.itheima.domain.vo.UserLikeVo;
import com.itheima.service.mongo.UserLIkeService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class userLikeServiceImpl implements UserLIkeService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //添加单方我喜欢
    @Override
    public void saveUserLike(UserLike userLike) {
        mongoTemplate.save(userLike);
    }

    //判断是否是相互喜欢
    @Override
    public boolean isMutualLike(Long userId, Long likeUserId) {
        //自己喜欢对方
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        boolean exists1 = mongoTemplate.exists(query, UserLike.class);

        //对方喜欢自己
        Query query1 = new Query(Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId));
        boolean exists2 = mongoTemplate.exists(query1, UserLike.class);
        return exists1 && exists2;
    }

    //删除喜欢关系
    //再次刷到，不喜欢，，就删掉
    @Override
    public void removeUserLike(Long userId, Long likeUserId) {
        if (mongoTemplate.exists(new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId)), UserLike.class)) {
            mongoTemplate.remove(new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId)), UserLike.class);
        }
    }

    //统计关系
    @Override
    public UserLikeCountVo findUserLikeCountVo(Long userId) {
        //互相喜欢
        long eachLoveCount = mongoTemplate.count(new Query(Criteria.where("userId").is(userId)), Friend.class);

        //喜欢
        long loveCount = mongoTemplate.count(new Query(Criteria.where("userId").is(userId)), UserLike.class);

        //粉丝
        long fanCount = mongoTemplate.count(new Query(Criteria.where("likeUserId").is(userId)), UserLike.class);

        UserLikeCountVo vo = new UserLikeCountVo();
        vo.setEachLoveCount(eachLoveCount);
        vo.setFanCount(fanCount);
        vo.setLoveCount(loveCount);

        return vo;
    }


    //我的详情
    @Override
    public PageBeanVo findUserLikeVoByType(Long userId, Integer type, Integer pageNum, Integer pageSize) {
        Integer index = (pageNum-1)*pageSize;

        ArrayList<UserLikeVo> userLikeVos = new ArrayList<>();
        long count = 0L;
        switch (type){
            case 1:{
                //相互喜欢
                Query query = new Query(Criteria.where("userId").is(userId))
                        .with(Sort.by(Sort.Order.desc("created")))
                        .skip(index).limit(pageSize);
                List<Friend> friendList = mongoTemplate.find(query, Friend.class);
                //只为了取出friendId，来给UserLikeVo封装
                if (CollectionUtil.isNotEmpty(friendList)) {
                    for (Friend friend : friendList) {
                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(friend.getFriendId());
                        userLikeVos.add(vo);
                    }
                }
                count = mongoTemplate.count(query, Friend.class);
                break;
            }case 2:{
                //喜欢我的
                Query query = new Query(Criteria.where("userId").is(userId))
                        .with(Sort.by(Sort.Order.desc("created")))
                        .skip(index).limit(pageSize);
                List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
                if (CollectionUtil.isNotEmpty(userLikes)) {
                    for (UserLike userLike : userLikes) {
                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(userLike.getLikeUserId());
                        userLikeVos.add(vo);
                    }
                }
                count = mongoTemplate.count(query, UserLike.class);
                break;
            }case 3:{
                //我喜欢的
                Query query = new Query(Criteria.where("likeUserId").is(userId))
                        .with(Sort.by(Sort.Order.desc("created")))
                        .skip(index).limit(pageSize);
                List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
                if (CollectionUtil.isNotEmpty(userLikes)) {
                    for (UserLike userLike : userLikes) {
                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(userLike.getUserId());
                        userLikeVos.add(vo);
                    }
                }
                count = mongoTemplate.count(query, UserLike.class);
                break;
            }case 4:{
                //谁看过我
                Query query = new Query(Criteria.where("userId").is(userId))
                        .with(Sort.by(Sort.Order.desc("date")))
                        .skip(index).limit(pageSize);
                List<Visitor> visitors = mongoTemplate.find(query, Visitor.class);
                if (CollectionUtil.isNotEmpty(visitors)) {
                    for (Visitor visitor : visitors) {
                        UserLikeVo userLikeVo = new UserLikeVo();
                        userLikeVo.setId(visitor.getVisitorUserId());
                        userLikeVos.add(userLikeVo);
                    }
                }
                count = mongoTemplate.count(query, Visitor.class);
                break;
            }
        }
        return new PageBeanVo(pageNum,pageSize,count,userLikeVos);
    }
}
